﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kokomo.Mirror.CliMetadata.Signatures;

namespace Kokomo.Mirror.CliMetadata.DebugInfo
{
	public class SymMethodScopeBuilder
	{
		internal SymMethodScopeBuilder(int startOffset, int length)
		{
			this.scopes = new List<SymMethodScopeBuilder>();
			this.namespaceImports = new List<string>();
			this.variables = new SortedList<int, SymVariableBuilder>();
		}

		public int StartOffset { get; private set; }
		public int Length { get; private set; }

		internal List<SymMethodScopeBuilder> scopes;
		public SymMethodScopeBuilder DefineScope(int startOffset, int length)
		{
			if (
				(startOffset < this.StartOffset)
				|| ((startOffset + length) > (this.StartOffset + this.Length))
				)
			{
				throw new ArgumentException("Inner scope must fall within bounds of the outer scope");
			}

			SymMethodScopeBuilder scope = new SymMethodScopeBuilder(startOffset, length);
			this.scopes.Add(scope);
			return scope;
		}

		internal List<string> namespaceImports;
		public void ImportNamespace(string @namespace)
		{
			this.namespaceImports.Add(@namespace);
		}

		internal SortedList<int, SymVariableBuilder> variables;
		public SymVariableBuilder DefineVariable(
			string name,
			byte[] signature,
			int localIndex
			)
		{
			SymVariableBuilder variable = new SymVariableBuilder(name, signature, localIndex);
			this.variables.Add(localIndex, variable);
			return variable;
		}
	}
}
